/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 var filechk;
function FileChecker(){

    var obj = this;
    this.addedList = new Array();
    this.ppath = 0;
    this.filetreecreated=0;
	this.ctlrkey=false;
    
    this.initialize = function(){        
        this.getSettings();                	            
        this.settingsDialogue();
        this.createGrid();
        this.createScanningDialogue();
    }

    this.createFileTree = function(folder){        
        if(this.filetreecreated)
            return true;
        $('#file_list').fileTree({ root: folder, script: 'src/jqueryFileTree.php',
           folderEvent: 'dblclick', expandSpeed: 750,
           collapseSpeed: 550, multiSelect:true, proot:this.ppath }, function(file) {

           }, function(){
                obj.createFileListContextMenu();
        });
        this.filetreecreated=1;
    }
	
    this.createFileListContextMenu = function(){
        $("#file_list UL LI A").contextMenu({
            menu: 'addMenu'
        },
        function(action, el, pos) {
            if(action=='ascan'){
				
                if(jQuery.inArray($(el).attr('rel'),obj.addedList)!=-1)
				
                    return false;
				$('#added_list UL').append('<li id="'+$(el).parent().attr('id')+'1'+'" class="'+$(el).parent().attr('class')+'"><a rel="'+$(el).attr('rel')+'" href="#" style="position: relative;" class="ui-draggable">'+$(el).text()+'</a></li>');                	obj.addedList[obj.addedList.length] = $(el).attr('rel');				
				
                obj.createScanListContextMenu();
                }else if(action=='sroot')
                    obj.setProjectRoot($(el).attr("rel"));
        });

    }
    
    this.addAllFiles = function(){
        $("#file_list .jqueryFileTree").children().map(
            function(){
                 if(jQuery.inArray($(this).find("A").attr('rel'),obj.addedList)!=-1)
                    return false;
                $('#added_list UL').append('<li id="'+$(this).find("A").parent().attr('id')+'1'+'" class="'+$(this).find("A").parent().attr('class')+'"><a rel="'+$(this).find("A").attr('rel')+'" href="#" style="position: relative;" class="ui-draggable">'+$(this).find("A").text()+'</a></li>');
                
				
					obj.addedList[obj.addedList.length] = $(this).find("A").attr('rel');
				
                obj.createScanListContextMenu();
            }
            );
    }
    this.createScanListContextMenu = function(){
        $("#added_list UL LI A").contextMenu({
            menu: 'removeMenu'
        },
        function(action, el, pos) {
            if(action=='delete'){
                $(el).remove();
                obj.addedList[jQuery.inArray($(el).attr('rel'),obj.addedList)] = null;
            }
        });
    }	
    
    
    this.scanFiles = function(){                    
        if(this.addedList.length==0){
            $("#added_list .jqueryFileTree").html('');
            return false;
        }
        qstring='';
        currentfile = this.addedList.shift();
        $(".flexigrid").attr("style","z-index:2000");
        $("#scanning").dialog("open");
        $("#scanning").attr("style","height:200px");
        $("#scanstatus").html('Checking... <br />' + currentfile);
        $(".ftitle").css('width','30%');
        $(".ftitle").css('float','left');
        $("#gmenu").remove();
        $(".mDiv").append('<div id="gmenu"><div id="save" style="float:left;cursor:pointer;text-align:center"><img src="images/page_save.png" /><br /> Save Result</div><div id="clear" style="float:left;cursor:pointer;text-align:center"><img src="images/bin_closed.png" /><br /> Clear Results</div></div>');
		$(".mDiv").append('<br/><br/><div style="float:right"> Show :<select name="sor" onChange="filechk.refineResults()">			  <option value="all">All</option><option value="var">Varied</option><option value="ide">Identical</option><option value="mser">Missing in Server</option><option value="mloc">Missing in Local</option></select></div');
        $("#clear").bind("click", function(e){            
            $("#flex1").flexReload({url: 'src/FileChecker.php?t=dtclear'});
            $("#flex1 tbody").html('');
            //$("#gmenu").remove();
        });
        qstring += 't=scan&f='+currentfile;   
        $("#added_list UL LI A[rel='"+currentfile+"']").attr('style','background:#BDF;');  
        $.ajax({
            type: "POST",
            url: "src/FileChecker.php?",
            data: qstring,
            success: function(msg){
                //alert(msg);                                
                $("#flex1").flexReload({url: 'src/FileChecker.php?t=dt'});
                $("#scanning").dialog("close");
                obj.removeFromScanList(currentfile);
                obj.scanFiles();
            }
        });
    }

    this.removeFromScanList = function(fil){        
        $("#added_list UL LI A[rel='"+fil+"']").unbind('mousedown').unbind('mouseup');
    }

    this.setProjectRoot = function(folder){
        qstring='t=sroot&f='+folder;
        $.ajax({
            type: "POST",
            url: "src/FileChecker.php",
            data: qstring,
            success: function(msg){
                obj.setSettings($.evalJSON(msg));
                obj.createFileTree(folder);
                obj.filetreecreated=1;
            }
        });
    }
    
    this.getSettings = function(){        
        qstring='t=gSet'
        $.ajax({
            type: "POST",
            url: "src/FileChecker.php",
            data: qstring,
            success: function(msg){                
                obj.setSettings($.evalJSON(msg));
            }
        });
    }

    this.setSettings = function(settings){     
 
        $("input[name='spath'][type='text']").attr('value',settings.serverpath);
        $("input[name='sfname'][type='text']").attr('value',settings.serverfilename);
        $("input[name='projectpath'][type='text']").attr('value',settings.projectpath);
        if(jQuery.trim($("input[name='spath'][type='text']").attr('value'))=='')
            $("#settings").dialog("open");			
            if(settings.prgpathnotset=='1'){
                this.ppath=0;
                $("h4[class='tit']").html( "<font color='red'>Project File Path not set. Select a folder and right click to set.</font>");
                $("#addMenu LI[class='copy']").hide();
                $("#addMenu LI[class='edit']").show();
                this.filetreecreated=0;
                this.createFileTree(settings.projectpath);
            }else{
                this.ppath=1;
                $("h4[class='tit']").html('Working directory :'+settings.projectpath);
                this.filetreecreated=0;
                $("#addMenu LI[class='copy']").show();
                $("#addMenu LI[class='edit']").hide();
                this.createFileTree(settings.projectpath);                
            }
    }

    this.showSettings = function(){
         $("#settings").dialog("open");
    }

    this.toggle = function(obj){        
        var stop=0;
        var ob = obj
        $(obj).nextAll().map(
            function(){        
                //alert($(this).find("td div ul li[class='directory expanded']").html());
                if(jQuery.trim($(this).find("td div ul li[class='directory expanded']").html()).length>0 ||
                    jQuery.trim($(this).find("td div ul li[class='directory collapsed']").html()).length>0)
                    stop=1;
                else if(jQuery.trim($(this).find("td div ul li ul li[class='file ext_txt']").html()).length>0 && stop==0)
                    if(this.style.display=="none"){
                        $(this).show();
                        $(obj).find("td div ul li[class='directory collapsed']").attr('class','directory expanded');
                    }else{
                        $(this).hide();                        
                        $(obj).find("td div ul li[class='directory expanded']").attr('class','directory collapsed');
                        
                    }
            }
        );
    }
    this.settingsDialogue = function(){
        $("#settings").dialog({
            bgiframe: true,
            modal: true,
            title: 'FileChecker',
            resizable: false,            
            autoOpen: false,
            closebutton: true,
            buttons: {
                    Submit: function() {
                        qstring='t=sSet'
                        qstring+="&settings[serverpath]="+jQuery.trim($("input[name='spath'][type='text']").attr('value'));
                        qstring+="&settings[serverfilename]="+jQuery.trim($("input[name='sfname'][type='text']").attr('value'));
                        qstring+="&settings[projectpath]="+jQuery.trim($("input[name='projectpath'][type='text']").attr('value'));
                        $.ajax({
                            type: "POST",
                            url: "src/FileChecker.php",
                            data: qstring,
                            success: function(msg){
                                obj.setSettings($.evalJSON(msg));
                                $("#settings").dialog('close');
                            }
                        });
                    },
                    
                    Cancel: function() {
                        $(this).dialog('close');
                    }
            }
        });
    }
    
    this.createScanningDialogue = function(){
        $("#scanning").dialog({
            width: '346px',
            height: '200px',
            bgiframe: true,
            modal: true,
            resizable: false,
            autoOpen: false,
            closable: false,
            lwindow: true,
            closebutton: false,
            position: 'top'
        });
    }
    
    this.createGrid = function(){
        
        $("#flex1").flexigrid
			(
			{
			url: 'src/FileChecker.php?t=dt',
			dataType: 'json',
			colModel : [
				{display: 'Name', name : 'scanname', width : 180, sortable : false, align: 'left'},
				{display: 'Item type', name : 'name', width : 180, sortable : false, align: 'left'},
				{display: 'Status', name : 'printable_name', width : 120, sortable : true, align: 'left'}
				],
			title: 'Scan Results',
			showTableToggleBtn: true
			}
			);
       

    }

}

    
$(document).ready( function() {
        filechk = new FileChecker();
        filechk.initialize();
});

